package com.css.fxfzysx.modules.lifeline.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.lifeline.param.ImportHubBuildingParam;
import com.css.fxfzysx.modules.lifeline.service.HubBuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("bubBuilding")
@Slf4j
public class HubBuildingController {

    @Autowired
    private HubBuildingService hubBuildingService;

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("importHubBuilding")
    public RestResponse importHubBuilding(@RequestParam("file") MultipartFile file,
                                          @RequestParam("type") Integer type,
                                          @RequestParam("province") String province,
                                          @RequestParam("city") String city,
                                          @RequestParam("county") String county,
                                          @RequestParam("cover") Integer cover,
                                          @RequestParam("id") String id,
                                          @RequestParam("fileIds")String fileIds) {
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                return RestResponse.fail("文件格式不支持!");
            }
            ImportHubBuildingParam param = new ImportHubBuildingParam();
            param.setType(type);
            param.setProvince(province);
            param.setCity(city);
            param.setCounty(county);
            param.setCover(cover);
            param.setId(id);
            param.setFileId(fileIds);
            return hubBuildingService.importHubBuilding(file,param);
        } catch (Exception e) {
            log.error("post importHubBuilding error:{}",e.getMessage(),e);
            return RestResponse.fail("导入失败，请检查模板内容的正确性!");
        }
    }

}
